package com.kgc.scd.controller;

import com.kgc.scd.annotation.RequestPermission;
import com.kgc.scd.annotation.RequestUser;
import com.kgc.scd.bean.Product;
import com.kgc.scd.service.ProductStockService;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import com.kgc.scd.util.UserAnno;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * Created On : 2022/6/16.
 * <p>
 * Author     : nainai
 * <p>
 * Description: 商品库存操作入口
 */
@Slf4j
@RestController
@Api(tags = "15、商品库存操作入口")
public class ProductStockController {

    @Autowired
    private ProductStockService productStockService;

    /**
     * @author : nainai
     * @date   : 2022/6/16
     * @param  : [com.kgc.scd.util.UserAnno, java.lang.String]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 订阅商品到货提醒
     */
    @RequestPermission
    @PostMapping("/subArrivalRemind")
    @ApiOperation(value = "订阅商品到货提醒", notes = "商品库存不为空，或者重复订阅返回订阅失败，支持统一返回")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "proId", value = "商品编号"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "SUCCESS"),
            @ApiResponse(code = 500, message = "FAIL")
    })
    public RequestResult<String> subArrivalRemind(@RequestUser UserAnno userAnno, @RequestParam String proId) {
        Map<String, String> map = productStockService.setArrivalRemind(userAnno.getUserId(), proId);

        if (map.get("result").equals("true")) {
            return ResultBuildUtil.success("订阅到货提醒成功！");
        }
        return ResultBuildUtil.fail(map.get("result"));
    }

    /**
     * @author : nainai
     * @date   : 2022/6/16
     * @param  : [com.kgc.scd.util.UserAnno]
     * @return : com.kgc.scd.util.RequestResult<java.util.List<com.kgc.scd.bean.Product>>
     * @description : 查询用户已订阅到货商品 
     */
    @RequestPermission
    @GetMapping("/getUserArrivalRemind")
    @ApiOperation(value = "查询用户订阅到货商品", notes = "支持统一返回")
    @ApiResponses({
            @ApiResponse(code = 200, message = "SUCCESS"),
            @ApiResponse(code = 500, message = "FAIL")
    })
    public RequestResult<List<Product>> getUserArrivalRemind(@RequestUser UserAnno userAnno) {
        List<Product> products = productStockService.queryUserArrivalRemind(userAnno.getUserId());
        if (products.isEmpty()) {
            Product product = Product.builder()
                    .proName("未订阅到货商品！")
                    .build();
            return ResultBuildUtil.fail(Arrays.asList(product));
        }

        return ResultBuildUtil.success(products);
    }

    /**
     * @author : nainai
     * @date   : 2022/6/17
     * @param  : [java.lang.String, java.lang.Integer]
     * @return : java.util.Map<java.lang.String,java.lang.String>
     * @description : 添加商品库存
     */
    @PostMapping("/addProductStock")
    @ApiOperation(value = "添加商品库存", notes = "支持统一返回")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "proId", value = "商品编号", type = "string"),
            @ApiImplicitParam(name = "stock", value = "库存数", type = "Integer")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "SUCCESS"),
            @ApiResponse(code = 500, message = "FAIL")
    })
    public RequestResult<String> addProductStock(@RequestParam("proId") String proId, @RequestParam("stock") Integer stock) {
        Map<String, String> map = productStockService.insertProductStock(proId, stock);
        return map.get("result").equals("true") ?
                ResultBuildUtil.success() :
                ResultBuildUtil.fail(map.get("result"));
    }
}
